package com.server.llh.llhserverwork.activity.my;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.base.LlhBaseNFCActivity;
import com.server.llh.llhserverwork.activity.work.SerTaskFragment;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.SerTransField;
import com.server.llh.llhserverwork.bean.UserDataInfoBean;
import com.server.llh.llhserverwork.port.SignInOrOutInterface;
import com.server.llh.llhserverwork.receiver.GetCardBroadCast;
import com.server.llh.llhserverwork.utils.Constant;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZTF on 2017-06-06.
 */

public class SignInActivity extends LlhBaseNFCActivity {
    private static final String TAG = "SignInActivity";
    @BindView(R.id.Sign_in)
    TextView SignIn;
    @BindView(R.id.main_radio)
    LinearLayout mainRadio;
    @BindView(R.id.client_card)
    TextView clientCard;
    @BindView(R.id.client_name)
    TextView clientName;
    @BindView(R.id.client_money)
    TextView clientMoney;
    @BindView(R.id.client_m)
    LinearLayout clientM;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.phone_view)
    LinearLayout phoneView;
    @BindView(R.id.client_address)
    TextView clientAddress;
    @BindView(R.id.address_show)
    LinearLayout addressShow;
    @BindView(R.id.show_oldman_info)
    LinearLayout showOldmanInfo;
    @BindView(R.id.my_signin)
    TextView mySignin;
    @BindView(R.id.succ_filse)
    TextView succFilse;
    GetCardBroadCast getCardBroadCast = null;
    static SignInOrOutInterface signInOrOutInterface;
    String waiterId, waiterOrgId, waiterOrgTypeId, userNameStr,
            waiterOrgName, serTransId,serSignRecordId, position,isHasField,customerId, packDetailId, transStatus, customerName, token;
    public static List<SerTransField> fieldLists=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        if (SerTaskFragment.fieldLists!=null){
            fieldLists=SerTaskFragment.fieldLists;
        }
        token = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        waiterId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.USER_ID, getApplicationContext());
        userNameStr = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.USER_NAMW,
                getApplicationContext());
        waiterOrgId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID,
                getApplicationContext());
        waiterOrgTypeId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_TYPE,
                getApplicationContext());
        waiterOrgName = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_NAME,
                getApplicationContext());
        if (getIntent() != null) {
            isHasField = getIntent().getStringExtra("isHasField");
            customerId = getIntent().getStringExtra("customerId");
            customerName = getIntent().getStringExtra("customerName");
            transStatus = getIntent().getStringExtra("transStatus");
            packDetailId = getIntent().getStringExtra("packDetailId");
            serTransId = getIntent().getStringExtra("serTransId");
            position = getIntent().getStringExtra("position");
            if (getIntent().getStringExtra("serSignRecordId")!=null){
                serSignRecordId = getIntent().getStringExtra("serSignRecordId");
            }
            clientMoney.setText(""+customerName);
        }

    }
    public static void registSignInAndOut(SignInOrOutInterface signInOrOutInter) {
        signInOrOutInterface = signInOrOutInter;
    }
    String cardId;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == -1000) {
                cardId = String.valueOf(msg.obj);
                Log.d(TAG, "cardId==" + cardId);
                clientCard.setText("" + cardId);
                getUserInfo(cardId, token);
//                T.showAnimToast(getApplicationContext(), "cardId==" + cardId);
            }
        }
    };
    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.READ_CARDACTION);
        getCardBroadCast = new GetCardBroadCast();
        getCardBroadCast.setmHandler(handler);
        getLbm().registerReceiver(getCardBroadCast, filter);
        super.onResume();
    }

    @OnClick({R.id.my_signin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_signin:
                if (customerName != null && customerName.equals(userName)) {

                } else {
                    succFilse.setText("客户信息不匹配，签到失败");
                }
                break;
        }
    }

    List<UserDataInfoBean.DataBean.AccountListBean> accountListBeen = new ArrayList<>();
    private String userName, userId, userOrgName, userOrgId;
    private int userOrgType;

    private void getUserInfo(String myCardId, String token) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("icNumber", myCardId);
        lRequest.addParam("token", token);

        lRequest.setUri(ServerPort.GET_CARD_INFO);
        lRequest.setIp(UrlsTools.REQ_URL);
        lRequest.setUriType("");
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), "服务器繁忙!");
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null) {
                    accountListBeen.clear();
                    Log.d(TAG, "dataBean ==" + responseBean.toString());
                    UserDataInfoBean userDataInfoBean = new Gson().fromJson(responseBean, UserDataInfoBean.class);
                    userName = userDataInfoBean.getData().getUserName();
                    userId = userDataInfoBean.getData().getUserId();
                    userOrgName = userDataInfoBean.getData().getUserOrgName();
                    userOrgType = userDataInfoBean.getData().getUserOrgType();
                    userOrgId = userDataInfoBean.getData().getUserOrgId();
                    if (TextUtils.isEmpty(userName) || "".equals(userName)) {
                        ToastUtil.showToast(getApplicationContext(), "抱歉,该卡信息不完整...");
                    } else {
                        clientName.setText("" + userName);
                    }
                    if (customerName != null && customerName.equals(userName)) {
                        succFilse.setText("成功！");
                        signInOrOutInterface.callBack(position,transStatus,packDetailId,serTransId,customerId,serSignRecordId,isHasField);
                        finish();
                    } else {
                        succFilse.setText("客户信息不匹配，签到失败！");
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (getCardBroadCast != null) {
            getLbm().unregisterReceiver(getCardBroadCast);
        }
        super.onDestroy();
    }

    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }

    @Override
    public void hideProgress() {
        try {
            closeProgress();
        } catch (Exception e) {

        }
    }

    @Override
    protected int getlayout() {
        return R.layout.activity_sign_in;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    @Override
    public void showProgress() {
        try {
            showProgress(SignInActivity.this);
        } catch (Exception e) {

        }
    }
}
